package cn.db101.api.complex.service

import cn.db101.api.complex.component.Slf4j
import cn.db101.api.complex.component.Slf4j.Companion.log
import cn.db101.api.complex.entity.WeatherConfig
import cn.db101.api.complex.mapper.WeatherConfigMapper
import cn.db101.api.complex.model.request.WeatherConfigExcelRequest
import cn.db101.api.complex.model.request.WeatherCurrentRequest
import cn.db101.api.complex.model.request.WeatherFutureRequest
import cn.db101.api.complex.model.response.WeatherCurrentResponse
import cn.db101.api.complex.model.response.WeatherFutureResponse
import cn.db101.api.complex.service.weather.ThirdWeather
import com.alibaba.excel.EasyExcel
import com.alibaba.excel.context.AnalysisContext
import com.alibaba.excel.read.listener.ReadListener
import com.mybatisflex.core.query.QueryWrapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

/**
 * @author zooy
 * @since 2024/8/21-11:54
 */
@Service
@Slf4j
class WeatherService {

    @Autowired
    private lateinit var thirdWeatherList: List<ThirdWeather>

    @Autowired
    private lateinit var weatherConfigMapper: WeatherConfigMapper

    fun current(weatherCurrentRequest: WeatherCurrentRequest): WeatherCurrentResponse? {

        for (i in 0 until thirdWeatherList.size) {
            val weatherCurrentResponse = thirdWeatherList[i].currentWeather(weatherCurrentRequest)
            if (weatherCurrentResponse!= null) {
                return weatherCurrentResponse
            }
        }
        return null
    }

    fun future(weatherFutureRequest: WeatherFutureRequest): WeatherFutureResponse? {

        for (i in 0 until thirdWeatherList.size) {
            val weatherFutureResponse = thirdWeatherList[i].futureWeather(weatherFutureRequest)
            if (weatherFutureResponse!= null) {
                return weatherFutureResponse
            }
        }
        return null
    }


    fun importConfig(mf: MultipartFile) {

        EasyExcel.read(
            mf.inputStream,
            WeatherConfigExcelRequest::class.java,
            object : ReadListener<WeatherConfigExcelRequest> {
                override fun invoke(
                    wcer: WeatherConfigExcelRequest,
                    analysisContext: AnalysisContext?
                ) {

                    try {
                        weatherConfigMapper.insert(WeatherConfig().also {
                            it.adcode = wcer.adcode
                            it.name = wcer.name
                            it.cityCode = wcer.cityCode
                        })
                    } catch (e: Exception) {
                        if(e is java.sql.SQLIntegrityConstraintViolationException) {
                            log.info("primary key repeat adcode: ${wcer.adcode}")
                        }
                    }

                }
                override fun doAfterAllAnalysed(analysisContext: com.alibaba.excel.context.AnalysisContext?) {

                }
            }).sheet().doRead()

    }

    fun cityDict(weatherConfig: WeatherConfig): List<WeatherConfig>? {

        return weatherConfigMapper.selectListByQuery(QueryWrapper.create()
            .eq(WeatherConfig::adcode, weatherConfig.adcode)
            .eq(WeatherConfig::cityCode, weatherConfig.cityCode)
        )
    }

}
